package com.liuche.book.auth.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuche.book.auth.dao.mapper.PermissionMapper;
import com.liuche.book.auth.dao.mapper.RolePermissionRelMapper;
import com.liuche.book.auth.model.entity.Permission;
import com.liuche.book.auth.model.entity.RolePermissionRel;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author：liuche
 * @Date：2024年07月07日 08:45
 * @Description：
 */
@Repository
public class RolePermissionDao extends ServiceImpl<RolePermissionRelMapper, RolePermissionRel> {
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 查询权限信息通过角色 id
     * @param commonUserRoleId 角色id
     * @return 权限信息
     */
    public List<Permission> queryRolePermissionInfo(Long commonUserRoleId) {
        // 得到 permission id
        LambdaQueryWrapper<RolePermissionRel> queryRelWrapper = new LambdaQueryWrapper<>();
        queryRelWrapper.eq(RolePermissionRel::getRoleId, commonUserRoleId);
        List<RolePermissionRel> rolePermissionRelList = this.baseMapper.selectList(queryRelWrapper);
        List<Long> list = rolePermissionRelList.stream().map(RolePermissionRel::getPermissionId).toList();

        // 得到 permission List
        LambdaQueryWrapper<Permission> queryPermissionWrapper = new LambdaQueryWrapper<>();
        queryPermissionWrapper.in(Permission::getId, list);
        return permissionMapper.selectList(queryPermissionWrapper);
    }

    /**
     * 批量查询根据权限id
     * @param roleIds
     * @return
     */
    public List<RolePermissionRel> queryByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<RolePermissionRel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RolePermissionRel::getRoleId, roleIds);

       return list(queryWrapper);
    }
}
